
package com.enjoyiot.module.bms.controller.admin.asset;

import com.enjoyiot.framework.common.enums.CommonStatusEnum;
import com.enjoyiot.framework.common.pojo.CommonResult;
import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import com.enjoyiot.module.bms.controller.admin.asset.vo.*;
import com.enjoyiot.module.bms.dal.dataobject.AssetDO;
import com.enjoyiot.module.bms.sevice.asset.AssetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import static com.enjoyiot.framework.common.pojo.CommonResult.success;

@Tag(name = "平台管理 - 资产信息")
@RestController
@RequestMapping("/bms/asset")
@Validated
public class BmsAssetController {

    @Resource
    private AssetService assetService;

    @PostMapping("/create")
    @Operation(summary = "创建资产信息")
    @PreAuthorize("@ss.hasPermission('bms:asset:create')")
    public CommonResult<Long> createAsset(@Valid @RequestBody AssetCreateReqVO createReqVO) {
        return success(assetService.createAsset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产信息")
    @PreAuthorize("@ss.hasPermission('bms:asset:update')")
    public CommonResult<Boolean> updateAsset(@Valid @RequestBody AssetUpdateReqVO updateReqVO) {
        assetService.updateAsset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bms:asset:delete')")
    public CommonResult<Boolean> deleteAsset(@RequestParam("id") Long id) {
        assetService.deleteAsset(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bms:asset:query')")
    public CommonResult<AssetRespVO> getAsset(@RequestParam("id") Long id) {
        AssetDO asset = assetService.getAsset(id);
        return success(BeanUtils.toBean(asset, AssetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产信息分页")
    @PreAuthorize("@ss.hasPermission('bms:asset:query')")
    public CommonResult<PageResult<AssetRespVO>> getAssetPage(@Valid AssetPageReqVO pageVO) {
        PageResult<AssetDO> pageResult = assetService.getAssetPage(pageVO);
        return success(BeanUtils.toBean(pageResult, AssetRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "资产状态修改")
    @PreAuthorize("@ss.hasPermission('bms:asset:update')")
    public CommonResult<Boolean> updateAssetStatus(@Valid @RequestBody AssetUpdateStatusReqVO reqVO) {
        assetService.updateAssetStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取资产精简信息列表", description = "只包含被开启的资产，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('bms:asset:query')")
    public CommonResult<List<AssetSimpleRespVO>> getSimpleAssetList(@RequestParam("assetName") String assetName) {
        // 获得资产列表，只要开启状态的，status=1，type=0的
        List<AssetDO> list = assetService.getSimpleAssetList(assetName);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(AssetDO::getCategoryId));
        return success(BeanUtils.toBean(list, AssetSimpleRespVO.class));
    }

}
